<template>
  <a-modal
    :title="formInfo.id==undefined?'新增':'修改'"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="$global.labelCol"
        :wrapper-col="$global.wrapperCol"
      >
            <a-row>
              <a-col :span="12">
            <a-form-model-item label="名称" prop="name" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.name" placeholder="请输入"/>
            </a-form-model-item>
          </a-col>
                  <a-col :span="12">
                      <a-form-model-item label="手机号" prop="mobile"  :maxLength="50">
                          <a-input v-model="formInfo.mobile" placeholder="请输入"/>
                      </a-form-model-item>
          </a-col>
            </a-row>
                <a-row>

                    <a-col :span="12" >
                        <a-form-model-item label="角色" prop="role"  :maxLength="50">
                           <!--{{this.$sysDictFilter(formInfo.role, 'roles')}}-->
                            <a-select v-model="formInfo.role" placeholder="请选择角色">
                                <a-select-option value="">请选择</a-select-option>
                                <a-select-option
                                        v-for="(item) in this.$sysDictOptions('roles')"
                                        :key="item.k"
                                        :value="parseInt(item.k)">
                                    {{ item.v }}
                                </a-select-option>
                            </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12" v-if="formInfo.role==1">
                        <a-form-model-item label="部门" prop="departCode"  :maxLength="50">
                           <!-- {{this.$getDepartmentName( formInfo.departCode)}}-->
                            <a-input
                                    v-model="formInfo.departCode"
                                    placeholder="请输入"
                                    type="hidden"/>
                            <depart-window-show
                                    :multiple-flag="false"
                                    :checkedDepartKeys="formInfo.departCode"
                                    @ok="departOk"
                            ></depart-window-show>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12" v-if="formInfo.role==2">
                        <a-form-model-item label="部门" prop="departCode"  :maxLength="50">
                            信息科
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12" v-if="formInfo.role==3">
                        <a-form-model-item label="厂家" prop="factorId"  :maxLength="50">
                           <!-- {{formInfo.factorName}}-->
                            <a-select v-model="formInfo.factorId" placeholder="请选择厂家" optionFilterProp="children" showSearch>
                                <a-select-option value="">请选择</a-select-option>
                                <a-select-option
                                        v-for="(item) in faclist"
                                        :key="item.id"
                                        :value="parseInt(item.id)">
                                    {{ item.fatName }}
                                </a-select-option>
                            </a-select>
                        </a-form-model-item>
                    </a-col>
            </a-row>
          <a-row>
              <a-col :span="12" v-if="formInfo.role==2">
                  <a-form-model-item label="后台账号" prop="companyName"  :maxLength="50">
                      <a-input v-model="companyName" placeholder="请输入" />
                  </a-form-model-item>
              </a-col>
          </a-row>

              </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  import DepartWindowShow from '@/components/WxDepartChoose/DepartWindowShow'
  import CompanyList from "../../base/company/CompanyList";
  export default {
    name: 'UserEdit',
    components: {
        CompanyList,
        DepartWindowShow
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
        formInfo: {
          name: undefined,
          idCard: undefined,
          status: undefined,
          depart: undefined,
          companyName:undefined,
            role:'',
            mobile: undefined,
            departCode:'',
            factorId:'',
             wx: ''
        },
          companyName:undefined,
          faclist:{},
        url: {
          saveInfo: '/system/user/save',
          getInfo: '/system/user/info',
            getfacList:'/system/hosFactor/listAll'
        }
      }
    },
    created () {
    },
    methods: {
        departOk (val) {
            this.formInfo.departCode = val
        },
      add () {
        this.edit()
      },
      edit (record) {
        this.$objToOverride(this.formInfo, record)
        if (this.formInfo.id) {
          getAction(this.url.getInfo, { 'id': record.id }).then(res => {
            this.$nextTick(() => {
                this.formInfo = res.result.data
                if(res.result.company) {
                    this.companyName = res.result.company.login
                }
            })
          })
        }
        this.getfaclist()
        this.visible = true
      },
        getfaclist(){
            postAction(this.url.getfacList, {  }).then(res => {
                this.$nextTick(() => {
                    this.faclist = res.result.data
                })
            })
        },
      // 确定
      handleSubmit () {
        // 触发表单验证
        this.confirmLoading = true
        this.$refs.ruleForm.validate(valid => {
          console.log(valid)
            if(this.formInfo.role==2){
                this.formInfo.departCode='A100000A003A025';
            }
            this.formInfo.companyName=this.companyName
          if (valid) {
            postAction(this.url.saveInfo, this.formInfo).then(res => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      // 关闭
      handleCancel () {
        this.close()
      },
      close () {
        this.$refs.ruleForm.resetFields()
        this.$objToReset(this.formInfo)
        this.$emit('close')
        this.visible = false
      },
      editorOk (val) {
        this.formInfo.content = val
      }
    }
  }
</script>
